<?php

/**
* This is the model class for table "tbl_comment".
*
* The followings are the available columns in table 'tbl_comment':
* @property integer $id
* @property string $content
* @property string $status
* @property string $create_time
* @property integer $author
* @property string $email
* @property string $url
* @property integer $post_id
*/
class Comment extends CActiveRecord
{
    const STATUS_PENDING=1;
    const STATUS_APPROVED=2;
    const STATUS_BANNED=3;

    /**
    * Returns the static model of the specified AR class.
    * @return Comment the static model class
    */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
    * @return string the associated database table name
    */
    public function tableName()
    {
        return 'tbl_comment';
    }
    public function getUrl()
    {
        return Yii::app()->createUrl('post/view', array(
        'id'=>$this->id,

        ));
    }
    /**
    * @return array validation rules for model attributes.
    */
    public function rules()
    {
        return array(
        array('content, author, email', 'required'),
        array('author, email, url', 'length', 'max'=>255),
        array('email','email'),
        array('url','url'),
        array('content, status', 'safe', 'on'=>'search'),
        );
    }

    /**
    * @return array relational rules.
    */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
        );
    }

    /**
    * @return array customized attribute labels (name=>label)
    */
    public function attributeLabels()
    {
        return array(
        'id' => 'ID',
        'content' => Yii::t('Site',"CONTENT"),
        'status' => Yii::t('Site',"STATUS"),
        'create_time' => Yii::t('Site',"CREATE DATE"),
        'author' => Yii::t('Site',"AUTHOR"),
        'email' => 'Email',
        'url' => 'Website',
        'post_id' => Yii::t('Site',"POST"),
        );
    }

    /**
    * Retrieves a list of models based on the current search/filter conditions.
    * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
    */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('status',$this->status,true);
        $criteria->compare('create_time',$this->create_time,true);
        $criteria->compare('author',$this->author);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('post_id',$this->post_id);

        return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        ));
    }
    protected function beforeSave()
    {
        if(parent::beforeSave())
        {
            if($this->isNewRecord)
                $this->create_time=time();
            return true;
        }
        else
            return false;
    }
    public function getStatusOptions($i=0)
    {
        /**
        * Funtion ger status for commnet 
        * It return a status if $i >0 and return all status if $i=0
        */
        $a= array(
        self::STATUS_PENDING=>'Pending',
        self::STATUS_APPROVED=>'Approved',
        self::STATUS_BANNED=>'Banned',
        );
        return $i ? $a[$i]:$a;
    }
    public function AjaxApprove()
    {
        /**
        * Function set status for comment in admin
        */

        if ($this->status==self::STATUS_PENDING) {
            $this->status=self::STATUS_APPROVED;
        }
        else {
            if($this->status==self::STATUS_APPROVED)
            {
                $this->status=self::STATUS_BANNED;            
            }
            else{
                $this->status=self::STATUS_PENDING;    
            }
        }
        $this->save();   


    }
    public function pendingCommentCount()
    {
        /**
        * Function count the numbers of comment have status is Pedding
        */
        $criteria = new CDbCriteria; 
        $criteria->condition='status = '.self::STATUS_PENDING;
        return Comment::model()->count($criteria);
    }
    public function findRecentComments($limit=10)
    {
        /**
        * Funtion find Recent Comments
        */
        return $this->with('post')->findAll(array(
        'condition'=>'t.status='.self::STATUS_APPROVED,
        'order'=>'t.create_time DESC',
        'limit'=>$limit,
        ));
    }
    public function getIconApprove($status_id)
    {
        /**
        * Funtion get Icon for CGridView with status_id
        * return an array properties button 
        */
        if($status_id=self::STATUS_PENDING)
        {
            return array(
            'imageUrl'=>'/blog/assets/bb8ea9ba/gridview/loading.gif',
            'url'=>'"?r=comment/approve&id=$data->id"',
            );
        }
        if($status_id=self::STATUS_APPROVED)
        {
            return array(
            'imageUrl'=>'/blog/assets/bb8ea9ba/gridview/approved.png',
            'url'=>'"?r=comment/approve&id=$data->id"',
            );
        }
        return array(
        'imageUrl'=>'/blog/assets/bb8ea9ba/gridview/banned.png',
        'url'=>'"?r=comment/approve&id=$data->id"',
        );
        

    }

}